<%--
  Created by IntelliJ IDEA.
  User: hejiahao
  Date: 2021/12/17
  Time: 11:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%
    Connection con = null;
    Statement sm = null;
    ResultSet rs = null;
    try {
        //注册加载一个Driver驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
//获取数据库连接，root是数据库服务器的用户名，123456是数据库服务器的密码
        String jdbcURL = "jdbc:mysql://127.0.0.1:3306/goodslist?characterEncoding=UTF-8";
        con = DriverManager.getConnection(jdbcURL,"root","123456");
        //创建SQL命令对象
        sm = con.createStatement();
        //通过SQL命令执行SQL语句返回数据库
        rs = sm.executeQuery("SELECT * FROM goods ;");
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }

%>
<html>
<head>
    <title>List</title>
</head>
<body>
<table border="1">
    <h1>Insert</h1>
    <tr >
        <td>goodscode</td><!--商品编号-->
        <td>goodsname</td><!--商品名字-->
        <td>specification</td><!--商品规格-->
        <td>prices</td><!--商品价格-->
        <td>describe</td><!--商品描述-->
    </tr>
    <%
        try {
            while (rs.next()) {
                    out.println("<tr>");
                    out.println("<td><a href=update.jsp? goodscode=" + rs.getString("goodscode") +"&goodsname=" + rs.getString("goodsname") +"&specification=" + rs.getString("specification") +"&prices=" + rs.getString("prices") +"&describe=" + rs.getString("describe") +">" + rs.getString("goodscode") + "</a></td>");
                    out.println("<td>" + rs.getString("goodsname") + "</td>");
                    out.println("<td>" + rs.getString("specification") + "</td>");
                    out.println("<td>" + rs.getString("prices") + "</td>");
                    out.println("<td>" + rs.getString("describe") + "</td>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String gs = null;
		String gn = null;
		String sp = null;
		String ps = null;
		String ds = null;
        try {
            gs = rs.getString("goodscode");
			gn = rs.getString("goodsname");
			sp = rs.getString("specification");
			ps = rs.getString("prices");
			ds = rs.getString("describe");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    %>
    <tr>
    <td><a href="delete.jsp? goodscode<%=gs%>"><input type="submit" value="delete"></a></td>
    </tr>
    <tr>
        <td><a href="insert.jsp? goodscode<%=gs%>&goodsname<%=gn%>&specification<%=sp%>&prices<%=ps%>&describe<%=ds%>"><input type="submit" value="insert"></a></td>
    </tr>
    <tr>
        <td>
            <a href="main.jsp"><input type="submit" value="back"></a>
        </td>
    </tr>

</table>
<%
    if (rs != null){
        rs.close();
    }
    if (sm != null){
        sm.close();
    }
    if (con != null){
        con.close();
    }
%>

</body>
</html>
